package com.lagou.zuoye.servlet;

import com.lagou.zuoye.service.StudentService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "DeleteServlet",urlPatterns = "/delete")
public class DeleteServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        System.out.println("从请求中获得的需要删除的学员的姓名是："+name);

        StudentService studentService = new StudentService();
        int i = studentService.StudentDeleteService(name);
        if(0==i){
            System.out.println("删除失败,请输入正确的学员姓名");
            request.setAttribute("deleteError","删除失败，输入的学员姓名不正确！");
            //实现服务器跳转，共享request和response    对象     使用转发的操作
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("list.jsp");
            requestDispatcher.forward(request,response);
        }else{
            System.out.println("删除成功!");
            request.getSession().setAttribute("name",name);
            response.sendRedirect("delete.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
